package com.tanhua.server.controller;


import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentsController {


    @Autowired
    private CommentsService commentsService;

    /*
     *  分页查询评论列表
     *    请求方式和请求链接 :GET
     *    请求参数: Query:
     *          movementId ,page,pagesize
     *    返回值:  CommentVo
     * */

    @GetMapping
    public ResponseEntity comments(String movementId,
                                   @RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize
    ){

        PageResult  result=commentsService.findAllComments(movementId,page,pagesize);

        return ResponseEntity.ok(result);

    }

    /*
     * 评论-提交
     *    请求方式和请求链接 : POST
     *    请求参数: Body: movementId,comment
     *    返回值: 无
     * */
    @PostMapping
    public ResponseEntity saveComment(@RequestBody Map map){

        //获得请求参数
        String movementId = (String) map.get("movementId");
        String comment = (String) map.get("comment");

        //调用commentsService保存数据
        commentsService.saveComment(movementId,comment);

        return ResponseEntity.ok(null);
    }

}
